<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="在单片机系统中应用按键的时候，如果只需要按下一次按键加 1 或减 1，那用第 8 章学到的知识就可以完成了，但如果想连续加很多数字的时候，要一次次按下这个按键确实有点不方便，这时我们会希望一直按住按键，数字就自动持续增加或减小，这就是所谓的长短按键应用。 当检测到一个按键产生按下动作后，马上执行一次相应的操作，同时在程序里记录按键按下的持续时间，该时间超过 1 秒后（主要是为了区别短按和长按这两个">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="10.5、单片机长短按键的应用">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu069/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="在单片机系统中应用按键的时候，如果只需要按下一次按键加 1 或减 1，那用第 8 章学到的知识就可以完成了，但如果想连续加很多数字的时候，要一次次按下这个按键确实有点不方便，这时我们会希望一直按住按键，数字就自动持续增加或减小，这就是所谓的长短按键应用。 当检测到一个按键产生按下动作后，马上执行一次相应的操作，同时在程序里记录按键按下的持续时间，该时间超过 1 秒后（主要是为了区别短按和长按这两个">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-27T05:06:54.732Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="10.5、单片机长短按键的应用">
<meta name="twitter:description" content="在单片机系统中应用按键的时候，如果只需要按下一次按键加 1 或减 1，那用第 8 章学到的知识就可以完成了，但如果想连续加很多数字的时候，要一次次按下这个按键确实有点不方便，这时我们会希望一直按住按键，数字就自动持续增加或减小，这就是所谓的长短按键应用。 当检测到一个按键产生按下动作后，马上执行一次相应的操作，同时在程序里记录按键按下的持续时间，该时间超过 1 秒后（主要是为了区别短按和长按这两个">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu069/">





  <title>10.5、单片机长短按键的应用 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu069/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">10.5、单片机长短按键的应用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:36:52+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>在单片机系统中应用按键的时候，如果只需要按下一次按键加 1 或减 1，那用第 8 章学到的知识就可以完成了，但如果想连续加很多数字的时候，要一次次按下这个按键确实有点不方便，这时我们会希望一直按住按键，数字就自动持续增加或减小，这就是所谓的长短按键应用。</p>
<p>当检测到一个按键产生按下动作后，马上执行一次相应的操作，同时在程序里记录按键按下的持续时间，该时间超过 1 秒后（主要是为了区别短按和长按这两个动作，因短按的时间通常都达到几百 ms），每隔 200ms（如果你需要更快那就用更短的时间，反之亦然）就自动再执行一次该按键对应的操作，这就是一个典型的长按键效果。</p>
<p>对此，我们做了一个模拟定时炸弹效果的实例，提供给大家作为参考。打开开关后，数码管显示数字 0，按向上的按键数字加 1，按向下的按键数字减 1，长按向上按键 1 秒后，数字会持续增加，长按向下按键 1 秒后，数字会持续减小。设定好数字后，按下回车按键，时间就会进行倒计时，当倒计时到 0 的时候，用蜂鸣器和板子上的 8 个 LED 小灯做炸弹效果，蜂鸣器持续响，LED 小灯全亮。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit BUZZ = P1^6;</span><br><span class="line">sbit ADDR3 = P1^3;</span><br><span class="line">sbit ENLED = P1^4;</span><br><span class="line">sbit KEY_IN_1 = P2^4;</span><br><span class="line">sbit KEY_IN_2 = P2^5;</span><br><span class="line">sbit KEY_IN_3 = P2^6;</span><br><span class="line">sbit KEY_IN_4 = P2^7;</span><br><span class="line">sbit KEY_OUT_1 = P2^3;</span><br><span class="line">sbit KEY_OUT_2 = P2^2;</span><br><span class="line">sbit KEY_OUT_3 = P2^1;</span><br><span class="line">sbit KEY_OUT_4 = P2^0;</span><br><span class="line">unsigned char code LedChar[] = &#123; //数码管显示字符转换表</span><br><span class="line">    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,</span><br><span class="line">    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E</span><br><span class="line">&#125;;</span><br><span class="line">unsigned char LedBuff[7] = &#123; //数码管+独立 LED 显示缓冲区</span><br><span class="line">    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF</span><br><span class="line">&#125;;</span><br><span class="line">unsigned char code KeyCodeMap[4][4] = &#123; //矩阵按键编号到标准键盘键码的映射表</span><br><span class="line">    &#123; 0x31, 0x32, 0x33, 0x26 &#125;, //数字键 1、数字键 2、数字键 3、向上键</span><br><span class="line">    &#123; 0x34, 0x35, 0x36, 0x25 &#125;, //数字键 4、数字键 5、数字键 6、向左键</span><br><span class="line">    &#123; 0x37, 0x38, 0x39, 0x28 &#125;, //数字键 7、数字键 8、数字键 9、向下键</span><br><span class="line">    &#123; 0x30, 0x1B, 0x0D, 0x27 &#125; //数字键 0、ESC 键、 回车键、 向右键</span><br><span class="line">&#125;;</span><br><span class="line">unsigned char KeySta[4][4] = &#123; //全部矩阵按键的当前状态</span><br><span class="line">    &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;</span><br><span class="line">&#125;;</span><br><span class="line">unsigned long pdata KeyDownTime[4][4] = &#123; //每个按键按下的持续时间，单位 ms</span><br><span class="line">    &#123;0, 0, 0, 0&#125;, &#123;0, 0, 0, 0&#125;, &#123;0, 0, 0, 0&#125;, &#123;0, 0, 0, 0&#125;</span><br><span class="line">&#125;;</span><br><span class="line">bit enBuzz = 0; //蜂鸣器使能标志</span><br><span class="line">bit flag1s = 0; //1 秒定时标志</span><br><span class="line">bit flagStart = 0; //倒计时启动标志</span><br><span class="line">unsigned char T0RH = 0; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0; //T0 重载值的低字节</span><br><span class="line">unsigned int CountDown = 0; //倒计时计数器</span><br><span class="line">void ConfigTimer0(unsigned int ms);</span><br><span class="line">void ShowNumber(unsigned long num);</span><br><span class="line">void KeyDriver();</span><br><span class="line">void main()&#123;</span><br><span class="line">    EA = 1; //使能总中断</span><br><span class="line">    ENLED = 0; //选择数码管和独立 LED</span><br><span class="line">    ADDR3 = 1;</span><br><span class="line">    ConfigTimer0(1); //配置 T0 定时 1ms</span><br><span class="line">    ShowNumber(0); //上电显示 0</span><br><span class="line">   </span><br><span class="line">    while (1)&#123;</span><br><span class="line">        KeyDriver(); //调用按键驱动函数</span><br><span class="line">        if (flagStart &amp;&amp; flag1s)&#123; //倒计时启动且 1 秒定时到达时，处理倒计时</span><br><span class="line">            flag1s = 0;</span><br><span class="line">            if (CountDown &gt; 0)&#123; //倒计时未到 0 时，计数器递减</span><br><span class="line">                CountDown--;</span><br><span class="line">                ShowNumber(CountDown); //刷新倒计时数显示</span><br><span class="line">                if (CountDown == 0)&#123; //减到 0 时，执行声光报警</span><br><span class="line">                    enBuzz = 1;</span><br><span class="line">                    //启动蜂鸣器发声</span><br><span class="line">                    LedBuff[6] = 0x00; //点亮独立 LED</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 配置并启动 T0，ms-T0 定时时间 */</span><br><span class="line">void ConfigTimer0(unsigned int ms)&#123;</span><br><span class="line">    unsigned long tmp; //临时变量</span><br><span class="line">    tmp = 11059200 / 12; //定时器计数频率</span><br><span class="line">    tmp = (tmp * ms) / 1000; //计算所需的计数值</span><br><span class="line">    tmp = 65536 - tmp; //计算定时器重载值</span><br><span class="line">    tmp = tmp + 28; //补偿中断响应延时造成的误差</span><br><span class="line">    T0RH = (unsigned char)(tmp&gt;&gt;8); //定时器重载值拆分为高低字节</span><br><span class="line">    T0RL = (unsigned char)tmp;</span><br><span class="line">    TMOD &amp;= 0xF0; //清零 T0 的控制位</span><br><span class="line">    TMOD |= 0x01; //配置 T0 为模式 1</span><br><span class="line">    TH0 = T0RH; //加载 T0 重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">&#125;</span><br><span class="line">    /* 将一个无符号长整型的数字显示到数码管上，num-待显示数字 */</span><br><span class="line">void ShowNumber(unsigned long num)&#123;</span><br><span class="line">    signed char i;</span><br><span class="line">    unsigned char buf[6];</span><br><span class="line">    for (i=0; i&lt;6; i++)&#123; //把长整型数转换为 6 位十进制的数组</span><br><span class="line">        buf[i] = num % 10;</span><br><span class="line">        num = num / 10;</span><br><span class="line">    &#125;</span><br><span class="line">    for (i=5; i&gt;=1; i--)&#123; //从最高位起，遇到 0 转换为空格，遇到非 0 则退出循环</span><br><span class="line">        if (buf[i] == 0)&#123;</span><br><span class="line">            LedBuff[i] = 0xFF;</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            break;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br><span class="line">    for ( ; i&gt;=0; i--)&#123; //剩余低位都如实转换为数码管显示字符</span><br><span class="line">        LedBuff[i] = LedChar[buf[i]];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 按键动作函数，根据键码执行相应的操作，keycode-按键键码 */</span><br><span class="line">void KeyAction(unsigned char keycode)&#123; //按键动作函数，根据键码执行相应动作</span><br><span class="line">    if (keycode == 0x26)&#123; //向上键，倒计时设定值递增</span><br><span class="line">        if (CountDown &lt; 9999)&#123; //最大计时 9999 秒</span><br><span class="line">            CountDown++;</span><br><span class="line">            ShowNumber(CountDown);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;else if (keycode == 0x28)&#123; //向下键，倒计时设定值递减</span><br><span class="line">        if (CountDown &gt; 1)&#123; //最小计时 1 秒</span><br><span class="line">            CountDown--;</span><br><span class="line">            ShowNumber(CountDown);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;else if (keycode == 0x0D)&#123; //回车键，启动倒计时</span><br><span class="line">        flagStart = 1; //启动倒计时</span><br><span class="line">    &#125;else if (keycode == 0x1B)&#123; //Esc 键，取消倒计时</span><br><span class="line">        enBuzz = 0; //关闭蜂鸣器</span><br><span class="line">        LedBuff[6] = 0xFF; //关闭独立 LED</span><br><span class="line">        flagStart = 0; //停止倒计时</span><br><span class="line">        CountDown = 0; //倒计时数归零</span><br><span class="line">        ShowNumber(CountDown);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 按键驱动函数，检测按键动作，调度相应动作函数，需在主循环中调用 */</span><br><span class="line">void KeyDriver()&#123;</span><br><span class="line">    unsigned char i, j;</span><br><span class="line">    static unsigned char pdata backup[4][4] = &#123; //按键值备份，保存前一次的值</span><br><span class="line">        &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    static unsigned long pdata TimeThr[4][4] = &#123; //快速输入执行的时间阈值</span><br><span class="line">        &#123;1000, 1000, 1000, 1000&#125;, &#123;1000, 1000, 1000, 1000&#125;,</span><br><span class="line">        &#123;1000, 1000, 1000, 1000&#125;, &#123;1000, 1000, 1000, 1000&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">   </span><br><span class="line">    for (i=0; i&lt;4; i++)&#123; //循环扫描 4*4 的矩阵按键</span><br><span class="line">        for (j=0; j&lt;4; j++)&#123;</span><br><span class="line">            if (backup[i][j] != KeySta[i][j])&#123; //检测按键动作</span><br><span class="line">                if (backup[i][j] != 0)&#123; //按键按下时执行动作</span><br><span class="line">                    KeyAction(KeyCodeMap[i][j]); //调用按键动作函数</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            backup[i][j] = KeySta[i][j];</span><br><span class="line">            if (KeyDownTime[i][j] &gt; 0)&#123;</span><br><span class="line">                if (KeyDownTime[i][j] &gt;= TimeThr[i][j])&#123; //达到阈值时执行一次动作</span><br><span class="line">                    KeyAction(KeyCodeMap[i][j]); //调用按键动作函数</span><br><span class="line">                    TimeThr[i][j] += 200; //时间阈值增加 200ms，以准备下次执行</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;else&#123; //按键弹起时复位阈值时间</span><br><span class="line">                TimeThr[i][j] = 1000; //恢复 1s 的初始阈值时间</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 按键扫描函数，需在定时中断中调用 */</span><br><span class="line">void KeyScan()&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">    static unsigned char keyout = 0; //矩阵按键扫描输出索引</span><br><span class="line">   </span><br><span class="line">    static unsigned char keybuf[4][4] = &#123; //矩阵按键扫描缓冲区</span><br><span class="line">        &#123;0xFF, 0xFF, 0xFF, 0xFF&#125;, &#123;0xFF, 0xFF, 0xFF, 0xFF&#125;,</span><br><span class="line">        &#123;0xFF, 0xFF, 0xFF, 0xFF&#125;, &#123;0xFF, 0xFF, 0xFF, 0xFF&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">   </span><br><span class="line">    //将一行的 4 个按键值移入缓冲区</span><br><span class="line">    keybuf[keyout][0] = (keybuf[keyout][0] &lt;&lt; 1) | KEY_IN_1;</span><br><span class="line">    keybuf[keyout][1] = (keybuf[keyout][1] &lt;&lt; 1) | KEY_IN_2;</span><br><span class="line">    keybuf[keyout][2] = (keybuf[keyout][2] &lt;&lt; 1) | KEY_IN_3;</span><br><span class="line">    keybuf[keyout][3] = (keybuf[keyout][3] &lt;&lt; 1) | KEY_IN_4;</span><br><span class="line">    //消抖后更新按键状态</span><br><span class="line">    for (i=0; i&lt;4; i++)&#123; //每行 4 个按键，所以循环 4 次</span><br><span class="line">        //连续 4 次扫描值为 0，即 4*4ms 内都是按下状态时，可认为按键已稳定的按下</span><br><span class="line">        if ((keybuf[keyout][i] &amp; 0x0F) == 0x00)&#123;</span><br><span class="line">            KeySta[keyout][i] = 0;</span><br><span class="line">            KeyDownTime[keyout][i] += 4; //按下的持续时间累加</span><br><span class="line">        //连续 4 次扫描值为 1，即 4*4ms 内都是弹起状态时，可认为按键已稳定的弹起</span><br><span class="line">        &#125;else if ((keybuf[keyout][i] &amp; 0x0F) == 0x0F)&#123;</span><br><span class="line">            KeySta[keyout][i] = 1;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    KeyDownTime[keyout][i] = 0; //按下的持续时间清零</span><br><span class="line">    //执行下一次的扫描输出</span><br><span class="line">    keyout++; //输出索引递增</span><br><span class="line">    keyout &amp;= 0x03; //索引值加到 4 即归零</span><br><span class="line">    switch (keyout)&#123; //根据索引，释放当前输出引脚，拉低下次的输出引脚</span><br><span class="line">        case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;</span><br><span class="line">        case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;</span><br><span class="line">        case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;</span><br><span class="line">        case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;</span><br><span class="line">        default: break;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* LED 动态扫描刷新函数，需在定时中断中调用 */</span><br><span class="line">void LedScan()&#123;</span><br><span class="line">    static unsigned char i = 0; //动态扫描索引</span><br><span class="line">    P0 = 0xFF; //关闭所有段选位，显示消隐</span><br><span class="line">    P1 = (P1 &amp; 0xF8) | i; //位选索引值赋值到 P1 口低 3 位</span><br><span class="line">    P0 = LedBuff[i]; //缓冲区中索引位置的数据送到 P0 口</span><br><span class="line">    if (i &lt; 6)&#123; //索引递增循环，遍历整个缓冲区</span><br><span class="line">        i++;</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        i = 0;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，完成数码管、按键扫描与秒定时 */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    static unsigned int tmr1s = 0; //1 秒定时器</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    if (enBuzz)&#123; //蜂鸣器发声处理</span><br><span class="line">        BUZZ = ~BUZZ; //驱动蜂鸣器发声</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        BUZZ = 1; //关闭蜂鸣器</span><br><span class="line">    &#125;</span><br><span class="line">    LedScan(); //LED 扫描显示</span><br><span class="line">    KeyScan(); //按键扫描</span><br><span class="line">    if (flagStart)&#123; //倒计时启动时处理 1 秒定时</span><br><span class="line">        tmr1s++;</span><br><span class="line">        if (tmr1s &gt;= 1000)&#123;</span><br><span class="line">            tmr1s = 0;</span><br><span class="line">            flag1s = 1;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;else&#123; //倒计时未启动时 1 秒定时器始终归零</span><br><span class="line">        tmr1s = 0;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>长按键功能实现的重点有两个：第一，是在原来的矩阵按键扫描函数 KeyScan 内，当检测到按键按下后，持续的对一个时间变量进行累加，其目的是用这个时间变量来记录按键按下的时间；第二，是在按键驱动函数 KeyDriver 里，除了原来的检测到按键按下这个动作时执行按键动作函数 KeyAction 外，还监测表示按键按下时间的变量，根据它的值来完成长按时的连续快速按键动作功能。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu068/" rel="next" title="10.4、51单片机RAM区域的划分">
                <i class="fa fa-chevron-left"></i> 10.4、51单片机RAM区域的划分
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu070/" rel="prev" title="11.1、单片机串行通信介绍">
                11.1、单片机串行通信介绍 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
